import 'package:flutter/material.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/buyer_entity.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/res/colors.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/utils/widget_generate_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/multi_state_widget/multi_controller.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:azlistview/azlistview.dart';
import 'package:module_common/page/buyer/choose_buyer_controller.dart';

class ChooseBuyerPage extends StatefulWidget {
  const ChooseBuyerPage({Key? key}) : super(key: key);

  @override
  _ChooseBuyerPageState createState() => _ChooseBuyerPageState();
}

class _ChooseBuyerPageState extends BaseState<ChooseBuyerPage, ChooseBuyerController> {
  final MultiController multiController = MultiController();

  @override
  void onCreate() {
    setTitle(MultiLanguage.buyer.tr);
    multiController.initStatus = MultiStateStatus.content;
  }

  @override
  ChooseBuyerController registerController() => Get.find<ChooseBuyerController>();

  @override
  List<Widget> getMainChildrenWidget(BuildContext context) {
    return [
      Expanded(
        child: Container(
          color: Colors.white,
          child: Column(
            children: [
              header(),
              Expanded(
                child: MultiStateWidget(
                  emptyWidget: Container(child: Text('暂无数据')),
                  controller: multiController,
                  child: Column(
                    children: [
                      Expanded(
                        child: AzListView(
                          data: !baseGetxController!.isAction.value
                              ? baseGetxController!.cityList
                              : baseGetxController!.cityListWithKeyWord,
                          itemCount: !baseGetxController!.isAction.value
                              ? baseGetxController!.cityList.length
                              : baseGetxController!.cityListWithKeyWord.length,
                          itemBuilder: (BuildContext context, int index) {
                            BuyerEntity model = !baseGetxController!.isAction.value
                                ? baseGetxController!.cityList[index]
                                : baseGetxController!.cityListWithKeyWord[index];
                            String tag = model.getSuspensionTag();
                            var isHasBottomLine = false;
                            if (!baseGetxController!.isAction.value) {
                              if ((baseGetxController!.cityList.length - 1) >= (index + 1)) {
                                BuyerEntity suppliersEntity =
                                    baseGetxController!.cityList[index + 1];
                                String nextTag = suppliersEntity.getSuspensionTag();
                                isHasBottomLine = nextTag == tag;
                              }
                            } else {
                              if ((baseGetxController!.cityListWithKeyWord.length - 1) >=
                                  (index + 1)) {
                                BuyerEntity suppliersEntity =
                                    baseGetxController!.cityListWithKeyWord[index + 1];
                                String nextTag = suppliersEntity.getSuspensionTag();
                                isHasBottomLine = nextTag == tag;
                              }
                            }
                            return GetBuilder<ChooseBuyerController>(builder: (logic) {
                              return GenerateWidgetUtil.getBuyerListItem(model,
                                  isHasBottomLine: isHasBottomLine, onTap: (data) {
                                baseGetxController!.suppliersEntity.value = data;
                                if (!baseGetxController!.isAction.value) {
                                  if (baseGetxController!.selectedPosition != -1) {
                                    baseGetxController!
                                        .cityList[baseGetxController!.selectedPosition]
                                        .isSelect = false;
                                    data.isSelect = true;
                                    baseGetxController!.selectedPosition = index;
                                  } else {
                                    baseGetxController!.selectedPosition = index;
                                    baseGetxController!
                                        .cityList[baseGetxController!.selectedPosition]
                                        .isSelect = true;
                                  }
                                } else {
                                  if (baseGetxController!.selectedIfWordIsEmptyPosition != -1) {
                                    baseGetxController!
                                        .cityListWithKeyWord[
                                            baseGetxController!.selectedIfWordIsEmptyPosition]
                                        .isSelect = false;
                                    data.isSelect = true;
                                    baseGetxController!.selectedPosition = index;
                                  } else {
                                    baseGetxController!.selectedIfWordIsEmptyPosition = index;
                                    baseGetxController!
                                        .cityListWithKeyWord[
                                            baseGetxController!.selectedIfWordIsEmptyPosition]
                                        .isSelect = true;
                                  }
                                }
                                baseGetxController!.update();
                              });
                            });
                          },
                          padding: EdgeInsets.zero,
                          susItemBuilder: (BuildContext context, int index) {
                            if (!baseGetxController!.isAction.value) {
                              BuyerEntity model = baseGetxController!.cityList[index];
                              String tag = model.getSuspensionTag();
                              return GenerateWidgetUtil.getSusItem(context, tag);
                            } else {
                              BuyerEntity model = baseGetxController!.cityListWithKeyWord[index];
                              String tag = model.getSuspensionTag();
                              return GenerateWidgetUtil.getSusItem(context, tag);
                            }
                          },
                          indexBarData: const [...kIndexBarData],
                        ),
                      ),
                      Container(
                          padding: EdgeInsets.only(top: 21.w, right: 24.w),
                          height: 166.w,
                          decoration: const BoxDecoration(color: Colors.white, boxShadow: [
                            BoxShadow(
                                offset: Offset(0.0, 5.0), //阴影xy轴偏移量
                                blurRadius: 15.0, //阴影模糊程度
                                spreadRadius: -11 //阴影扩散程度
                                )
                          ]),
                          alignment: Alignment.topRight,
                          child: GetBuilder<ChooseBuyerController>(
                            builder: (logic) {
                              return GradientButton(
                                ThemeUtil.getTheme(),
                                width: 184.w,
                                height: 77.w,
                                text: MultiLanguage.determine.tr,
                                onPressed: baseGetxController!.suppliersEntity.value == null
                                    ? null
                                    : baseGetxController!.changeBuyer,
                              );
                            },
                          ))
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    ];
  }

  Widget header() {
    return Container(
      color: ThemeUtil.getTheme().colorScheme.secondary,
      height: 116.h,
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
                alignment: Alignment.center,
                height: double.infinity,
                width: double.infinity,
                margin: EdgeInsets.only(left: 24.w, top: 24.w, bottom: 24.w),
                decoration: BoxDecoration(
                  color: ThemeUtil.getTheme().secondaryBgColor(),
                  borderRadius: const BorderRadius.all(Radius.circular(30.0)),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Gaps.hGap24,
                    LocalImage('admin_search',
                        package: Constant.packAdmin(), width: 26.w, height: 26.w),
                    Gaps.hGap24,
                    Expanded(
                      child: Container(
                        alignment: Alignment.center,
                        height: double.infinity,
                        width: double.infinity,
                        child: TextField(
                          onChanged: (value) {
                            if (baseGetxController!.keyWord.value != value) {
                              baseGetxController!.keyWord.value = value;
                              baseGetxController!.update();
                            }
                          },
                          autofocus: false,
                          controller: baseGetxController!.keyWorkSearchController,
                          textAlignVertical: TextAlignVertical.center,
                          decoration: InputDecoration(
                            focusedBorder: const OutlineInputBorder(
                                borderSide: BorderSide(width: 0, color: Colors.transparent)),
                            disabledBorder: const OutlineInputBorder(
                                borderSide: BorderSide(width: 0, color: Colors.transparent)),
                            enabledBorder: const OutlineInputBorder(
                                borderSide: BorderSide(width: 0, color: Colors.transparent)),
                            contentPadding: const EdgeInsets.symmetric(vertical: 0),
                            border: InputBorder.none,
                            labelStyle: TextStyles.textNormal28,
                            hintText: MultiLanguage.pleaseEnterBuyer.tr,
                            hintStyle: TextStyles.textSixthColor28,
                            suffixIcon: GetBuilder<ChooseBuyerController>(builder: (logic) {
                              return baseGetxController!.keyWord.isNotEmpty
                                  ? IconButton(
                                      iconSize: 30.w,
                                      icon: Icon(Icons.close, color: Colours.gray_400, size: 30.w),
                                      onPressed: () {
                                        baseGetxController!.isAction.value = false;

                                        baseGetxController!.suppliersEntity.value = null;

                                        baseGetxController!.keyWorkSearchController.text = '';
                                        baseGetxController!.keyWord.value = "";

                                        if (baseGetxController!.selectedIfWordIsEmptyPosition !=
                                            -1) {
                                          baseGetxController!
                                              .cityListWithKeyWord[
                                                  baseGetxController!.selectedIfWordIsEmptyPosition]
                                              .isSelect = false;
                                        }
                                        baseGetxController!.selectedIfWordIsEmptyPosition = -1;
                                        baseGetxController!.selectedPosition = -1;
                                        multiController.viewStateChange(MultiStateStatus.content);

                                        setState(() {});
                                      },
                                    )
                                  : Gaps.empty;
                            }),
                          ),
                        ),
                      ),
                    ),
                  ],
                )),
          ),
          textInkBgWell(MultiLanguage.search.tr, onTap: (action) {
            baseGetxController!.suppliersEntity.value = null;

            if (baseGetxController!.keyWord.isEmpty) {
              ToastUtil.waring(MultiLanguage.pleaseEnterSupplier.tr);
              return;
            }
            baseGetxController!.isAction.value = true;

            if (baseGetxController!.selectedPosition != -1) {
              baseGetxController!.cityList[baseGetxController!.selectedPosition].isSelect = false;
            }
            baseGetxController!.selectedIfWordIsEmptyPosition = -1;
            baseGetxController!.selectedPosition = -1;

            var keyWordCities = baseGetxController!.cityList.where((e) {
              return e.nickname.contains(baseGetxController!.keyWord.value);
            }).toList();
            baseGetxController!.cityListWithKeyWord.value = keyWordCities;
            if (baseGetxController!.cityListWithKeyWord.isEmpty) {
              multiController.viewStateChange(MultiStateStatus.empty);
            } else {
              ToastUtil.success(MultiLanguage.loadingCompleted.tr);
              multiController.viewStateChange(MultiStateStatus.content);
            }
            setState(() {});
          })
        ],
      ),
    );
  }
}
